/**
 * 封装导出Excal文件请求
 * @param url
 * @param data
 * @returns {Promise}
 */
import axios from 'axios'
import Cookies from "js-cookie"
import BASEURL from './baseUrl'
export function exportExcel (url, options = {}, type) {
  return new Promise((resolve, reject) =>{
    axios.defaults.headers['content-type'] = 'application/json;charset=UTF-8'
    axios({
      url: BASEURL + url,
      method: 'POST',
      data: options,
      responseType: 'blob',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'token':  Cookies.get('token'),
        'Authorization': Cookies.get('authorization')
      }
    }).then(response => {
      resolve(response.data)
      let blob = new Blob([response.data], {
        type: 'application/vnd.ms-excel'
      })
      let fileName = ''
      if (type === 1) {
        fileName = Date.parse(new Date()) + '.xls'
      } else {
        fileName = Date.parse(new Date()) + '.xlsx'
      }
      if (window.navigator.msSaveOrOpenBlob) {
        // console.log(2)
        navigator.msSaveBlob(blob, fileName)
      } else {
        // console.log(3)
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = fileName
        link.click()
        //释放内存
        window.URL.revokeObjectURL(link.href)
      }
    }, err => reject(err)).catch(err  => {
      reject(err)
    } )
  })
}
